package booking.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import booking.misc.Airport;
public class AirportDialog extends JDialog implements ActionListener {
    
    private static final String OK = "ok";
    private static final String CANCEL = "cancel";
    
    Airport    airport = null;
    JTextField nameFld;
    JTextField cityFld;
    JTextField countryFld;
    
    public AirportDialog(JFrame frame, Airport airport) {
        this(frame);
        this.airport = airport;
        nameFld.setText(airport.getAirportName());
        cityFld.setText(airport.getAirportCity());
        countryFld.setText(airport.getAirportCountry());
    }
    
    public AirportDialog(JFrame frame) {
        setTitle("Add Airport");
        setModal(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(frame);
        JPanel dataPnl = new JPanel(new GridLayout(3, 2, 5, 5));
        dataPnl.setBorder(new EmptyBorder(5, 5, 5, 5));
        add(dataPnl, BorderLayout.CENTER);
        dataPnl.add(new JLabel("Airport:"));
        nameFld = new JTextField(10);
        dataPnl.add(nameFld);
        dataPnl.add(new JLabel("City:"));
        cityFld = new JTextField(10);
        dataPnl.add(cityFld);
        dataPnl.add(new JLabel("Country:"));
        countryFld = new JTextField(10);
        dataPnl.add(countryFld);
        
        JPanel bottomPnl = new JPanel();
        add(bottomPnl, BorderLayout.PAGE_END);
        
        JButton btn = new JButton("OK");
        btn.setActionCommand(OK);
        btn.addActionListener(this);
        bottomPnl.add(btn);
        btn = new JButton("Cancel");
        btn.setActionCommand(CANCEL);
        btn.addActionListener(this);
        bottomPnl.add(btn);
        
        pack();
    }
    
    public Airport getAirport() {
        return airport;
    }
    public void actionPerformed(ActionEvent event) {
        String ac = event.getActionCommand();
        if (ac.equals(OK)) {
        	
            String airportName = nameFld.getText().trim();
            String airportCity = cityFld.getText().trim();
            String airportCountry = countryFld.getText().trim();
            
            if (airportName.equals("") || airportCity.equals("") || airportCountry.equals(""))
            	JOptionPane.showMessageDialog(this, "Felaktig inlogging", "FEL",
    					JOptionPane.ERROR_MESSAGE);
            	
            else {
            	airport = new Airport(-1, airportName, airportCity, airportCountry);
            }
            dispose();
        }
        else if (ac.equals(CANCEL)) {
            airport = null;
            dispose();
        }
    }
}

